package com.qcit.common.view
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jcodecraeer.xrecyclerview.ProgressStyle
import com.jcodecraeer.xrecyclerview.XRecyclerView
import com.qcit.common.R
import com.tubb.smrv.SwipeMenuHelper


open class SuperRecyclerView : XRecyclerView, SwipeMenuHelper.Callback {
    val onLoadListener: OnLoadListener? = null
    protected var mHelper: SwipeMenuHelper? = null
    var enable_swipemenu:Boolean=false
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initView()
    }


    fun initView() {
        mHelper = SwipeMenuHelper(context, this)
        layoutManager = LinearLayoutManager(context)
        this.setRefreshProgressStyle(ProgressStyle.BallScaleRipple)
        this.setLoadingMoreProgressStyle(ProgressStyle.BallScaleRipple)
        this.defaultRefreshHeaderView.setArrowImageView(R.drawable.ic_superrcv_refresh_arrow)
    }

    fun enableSwipeMenu(enable:Boolean){
        this.enable_swipemenu=enable
    }

    fun complete() {
        this.loadMoreComplete()
        this.refreshComplete()
    }

    fun completeRefresh() {
        this.refreshComplete()
    }

    fun completeLoadmore() {
        this.loadMoreComplete()
    }

    fun loadBottom(bottom:Boolean){
        this.setNoMore(bottom)
    }
    fun setOnLoadListener(onLoadListener: OnLoadListener?) {
        onLoadListener?.let {
            this.setLoadingListener(object : LoadingListener {
                override fun onRefresh() {
                    it.OnRefresh(this@SuperRecyclerView)
                }
                override fun onLoadMore() {
                    it.OnLoadMore(this@SuperRecyclerView)
                }
            })
        }
    }


    interface OnLoadListener {
        fun OnRefresh(superRecyclerView: SuperRecyclerView)
        fun OnLoadMore(superRecyclerView: SuperRecyclerView)
    }


    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if(enable_swipemenu) {
            return mHelper!!.handleDispatchTouchEvent(ev) || super.dispatchTouchEvent(ev)
        }else{
            return super.dispatchTouchEvent(ev)
        }
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        if(!enable_swipemenu)
            return super.onInterceptTouchEvent(event)
        var isIntercepted = super.onInterceptTouchEvent(event)
        val action = event.actionMasked
        when (action) {
            MotionEvent.ACTION_DOWN -> isIntercepted =
                mHelper!!.handleListDownTouchEvent(event, isIntercepted)
        }
        return isIntercepted
    }

    override fun getPositionForView(touchView: View?): Int {
        return getChildAdapterPosition(touchView!!)
    }

    override fun getRealChildCount(): Int {
        return childCount
    }

    override fun getRealChildAt(index: Int): View? {
        return getChildAt(index)
    }

    override fun transformTouchingView(touchingPosition: Int, touchingView: View?): View? {
        val vh = findViewHolderForAdapterPosition(touchingPosition)
        return vh?.itemView ?: touchingView
    }

    override fun setAdapter(adapter: Adapter<*>?) {
        mHelper!!.reset()
        super.setAdapter(adapter)
    }

    override fun reset() {
        mHelper!!.reset()
    }
}